package com.cnswhy.cloud.server.core.channel.hcpay.service.impl;

import com.alibaba.fastjson.JSON;
import com.cnswhy.cloud.common.core.exception.BizException;
import com.cnswhy.cloud.common.core.utils.Md5Utils;
import com.cnswhy.cloud.server.core.channel.hcpay.dto.*;
import com.cnswhy.cloud.server.core.channel.hcpay.entity.ResponseEntity;
import com.cnswhy.cloud.server.core.channel.hcpay.service.HcpayService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class HcpayServiceImpl implements HcpayService {

    private static final String success = "0000";
    private static final String inprocess = "0100";
    private static final int failure = 0001;

    @Override
    public QueryBankCardResponseDto queryBankCard(QueryBankCardDto queryBankCardDto, String merchId,String privateKey) throws BizException {
        String response = SdkRequest.requestActions("queryBankCard", JSON.toJSONString(queryBankCardDto), getBranchCode(merchId), merchId, getKey(merchId), privateKey);
        ResponseEntity responseEntity = JSON.parseObject(response, ResponseEntity.class);
        if (!success.equals(responseEntity.getCode())) {
            throw new BizException(failure, responseEntity.getMessage());
        }
        QueryBankCardResponseDto queryBankCardResponseDto = JSON.parseObject(responseEntity.getData(), QueryBankCardResponseDto.class);
        log.info(queryBankCardResponseDto.toString());
        return queryBankCardResponseDto;
    }

    @Override
    public BankCardVerifyResponseDto bankCardVerify(BankCardVerifyDto bankCardVerifyDto, String merchId) throws BizException {
        String response = SdkRequest.requestActions("bankCardVerify", JSON.toJSONString(bankCardVerifyDto), getBranchCode(merchId), merchId, getKey(merchId), getPrivateKey(merchId));
        ResponseEntity responseEntity = JSON.parseObject(response, ResponseEntity.class);
        if (!success.equals(responseEntity.getCode())) {
            throw new BizException(failure, responseEntity.getMessage());
        }
        BankCardVerifyResponseDto bankCardVerifyResponseDto = JSON.parseObject(responseEntity.getData(), BankCardVerifyResponseDto.class);
        return bankCardVerifyResponseDto;
    }


    @Override
    public TransQueryResponseDto transQuery(TransQueryDto transQueryDto, String merchId,String privateKey) throws BizException {
        String response = SdkRequest.requestActions("transQuery", JSON.toJSONString(transQueryDto), getBranchCode(merchId), merchId, getKey(merchId), privateKey);
        ResponseEntity responseEntity = JSON.parseObject(response, ResponseEntity.class);
        if (!success.equals(responseEntity.getCode())) {
            throw new BizException(failure, responseEntity.getMessage());
        }
        TransQueryResponseDto transQueryResponseDto = JSON.parseObject(responseEntity.getData(), TransQueryResponseDto.class);
        log.info(transQueryResponseDto.toString());
        return transQueryResponseDto;
    }


    @Override
    public ResponseEntity daifu(DaifuDto daifuDto, String merchId,String privateKey) throws BizException {
        String response = SdkRequest.requestActions("daifu", JSON.toJSONString(daifuDto), getBranchCode(merchId), merchId, getKey(merchId), privateKey);
        ResponseEntity responseEntity = JSON.parseObject(response, ResponseEntity.class);
        if (!success.equals(responseEntity.getCode())) {
            throw new BizException(failure, responseEntity.getMessage());
        }
        return responseEntity;
    }
    @Override
    public SubMerchRegisterResponseDto subMerchRegister(SubMerchRegisterDto subMerchRegisterDto, String merchId,String privateKey) throws BizException {
        String response = SdkRequest.requestActions("subMerchRegister", JSON.toJSONString(subMerchRegisterDto), getBranchCode(merchId), merchId, getKey(merchId), privateKey);
        ResponseEntity responseEntity = JSON.parseObject(response, ResponseEntity.class);
        if (!success.equals(responseEntity.getCode())) {
            throw new BizException(failure, responseEntity.getMessage());
        }
        SubMerchRegisterResponseDto subMerchRegisterResponseDto = JSON.parseObject(responseEntity.getData(), SubMerchRegisterResponseDto.class);
        return subMerchRegisterResponseDto;
    }

    @Override
    public ResponseEntity subMerchUpgrade(SubMerchUpgradeDto subMerchUpgradeDto, String merchId,String privateKey) throws BizException {
        String response = SdkRequest.requestActions("subMerchUpgrade", JSON.toJSONString(subMerchUpgradeDto), getBranchCode(merchId), merchId, getKey(merchId), privateKey);
        ResponseEntity responseEntity = JSON.parseObject(response, ResponseEntity.class);
        if(!success.equals(responseEntity.getCode())){
            throw new BizException(failure,responseEntity.getMessage());
        }
        return responseEntity;
    }

    @Override
    public ResponseEntity subPayApplySms(SubPayApplySmsDto subPayApplySmsDto, String merchId,String privateKey) throws BizException {
        String response = SdkRequest.requestActions("subPayApplySms", JSON.toJSONString(subPayApplySmsDto), getBranchCode(merchId), merchId, getKey(merchId), privateKey);
        ResponseEntity responseEntity = JSON.parseObject(response, ResponseEntity.class);
        if(!success.equals(responseEntity.getCode())){
            throw new BizException(failure,responseEntity.getMessage());
        }
        return responseEntity;
    }

    @Override
    public ResponseEntity subPayConfirm(SubPayConfirmDto subPayConfirmDto, String merchId,String privateKey) throws BizException {
        String response = SdkRequest.requestActions("subPayConfirm", JSON.toJSONString(subPayConfirmDto), getBranchCode(merchId), merchId, getKey(merchId), privateKey);
        ResponseEntity responseEntity = JSON.parseObject(response, ResponseEntity.class);
        if(!success.equals(responseEntity.getCode())){
            throw new BizException(failure,responseEntity.getMessage());
        }
        return responseEntity;
    }

    private String getKey(String merchId) {
        return Md5Utils.md5Hex(merchId);
    }

    private String getPrivateKey(String merchId) {
        if ("882030315003046".equals(merchId)) {
            return "MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAJaiZ6fDkrkTpTbzGjDEganwVY6hIM9bBqjYdGV6NiMAnWbBIBLrz6N4V+xzvvzilOSK9PIaxBEgLrJtNwlCzivW0yjK/G26SL620mGW0qkYIjG3yJLBptaBvCk/6QEuPKHwgH1ppfskFtXVXiATqm0sutSHw3pXiX62g1isCivBAgMBAAECgYAMRFN+pP2Y1tG4hNmYKRiKF0lQYSzD1hzXuTDZZrdr6EvpJ0cOY2e4Xz+BwPdHrsgDSJGj+awH3QAQhbZaqbcSWOFXLBVMRmr8JDzWmhgM8qmyuEL3qjDOJrJ5e08gOx27vzuPi8llU3WyIzScW3oKZiVZlKgSdIEgo99SEUskAQJBAMjWHZIaX8b3LMqUmb/ilTzt272LPJYAwJ+voPDT6y9IC7cGfZIiGegK/TTmF3WzQpzVTJyin6SZYth6dXt/AGECQQDAAlFUZL9YnM2t1lLNphxDpUejJYAiUhQuGDpTLarHK+Y8MifIHtsaCV8JAEft6E1s2EHWnXeekmwdvJ9uRGdhAkEAigFn7H+nFOfApawbQpoGhwnDl6nYBhOaSaC77QESeTj+o9QX8ZTxTFxQxkreEAkQWwjPTksJfGfSXoM3u5b3wQJBAIX8GAywEOYg5VW9DSL8GxUJLKgn7vYEGJjhxzFr2JQLiinPRftbrMDKcUqyq37Ub9LYRXkhgOtieZd2nDox+YECQHyCyNokD9to000zt/sjWMZ5q2Txh9glLiUK+kmDeSc4bYSV5Hvh8GR3bOR9654hOEJBgca+VYYQRUPii5JZgQw=";
        }
        if ("882035716004024".equals(merchId)) {
            return "MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAIwWEiRiyz8ooIsxV0PfTOFoK2ug1cE5ut+r7nLEUWNdJkMznKcx6OxQur8jHOjH2mEtbCNonN0EHAgib641713K25bXIo81R4+o/X1OHrZx6a+tKur1mwxBRv9BmqJJZV9OMu9Zlwrx+98TkvnTuMeCJUN9aAq0I6/Xzr9pbM5zAgMBAAECgYAFic3X0GwWqCWIuz+m+EoS00p8K9Snmf+wXhzEPsy07/bWS1ETLx71UM/BRMMc1OHmW6Rd1gH1nvb4v1vI+GrpLB3c4d7bPDk8T+ark5vWy4DgVN+INkTrXs55So1Jm0gxtgOAR7Av7PTgucVC9CeoW6SJu+BROgftqmJ2coIsMQJBAOW9adnGH2dA8txeOWdzv/L95kFJi+x8PL4L38SfvqA+Yn5Ulm66pY18n93Sb5HUoiNYTO/tswqwHbgHcpXY0BsCQQCcGTxUoWQjWHe8Bl5znpKSUato7gJ58OcOELoDsN6qD6usedXJUgKscgnkCvdk5/LaHiR0kwh8YbejeJH951CJAkB56EC65n7Tc4trlDQxySIZ9tCv5C7zlhlFX1emJn4o2fG5x3jr2O7wXSkXtAqv/3zp9Hke96sCCOI8PPd4vEvxAkAg43BCpH5ktA8GHpFn6tEW4v4qbXs4FfmYjQ2kGW/QhNy2yjSw5gaKLrzGv0jAid4w9ojXxuLS0gPO/WhFBqeBAkEA1KDtD/zmN8en0mZX3YGMH2RzHMJ5sL1KaN8Gq/L9uOWnZsEFnLvKVYVU3VCfHmHj3TrJz2zrXpss0NT99UKqPw==";
        }
        return "";
    }

    private String getBranchCode(String merchId) {
        return "661936014000001";
    }
}
